/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * <p>
 * PmCalendarService
 * 项目日历
 * <p>
 * 修改纪录
 * 2020-05-16 版本：1.0 wushenghong 创建。
 *
 * @version 版本：1.0
 * @author 作者：wushenghong
 * 创建日期：2020-05-16
 */
package com.topscomm.pm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.CboSystemConst;
import com.topscomm.pm.pojo.PmCalendarEntity;
import com.topscomm.pm.service.IPmCalendarService;
import com.topscomm.pm.service.impl.auto.PmCalendarServiceAuto;
import com.topscomm.pub.util.ConvertUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.*;

@Service(value = "pmCalendarService")
@Scope(value = "singleton")
public class PmCalendarService extends PmCalendarServiceAuto implements IPmCalendarService {
    /**
     * @param paraMap
     * @author wushenghong
     * @date 2020年5月9日
     * @description TODO 根据项目立项信息批量生成项目日历
     */
    @Override
    public void insertBatchCalendar(Map paraMap) throws ServiceException {
        long projectid = ConvertUtil.convertToLong(paraMap.get("projectid"));//项目
        String projectcode = ConvertUtil.convertToString(paraMap.get("projectcode"));//项目编号
        Date startdate = ConvertUtil.convertToDate(paraMap.get("starttime"));//项目开工时间
        Date enddate = ConvertUtil.convertToDate(paraMap.get("endtime"));//项目完工时间
        if (0 == projectid || "".equals(projectcode) || startdate == null || enddate == null) {
            throw new ServiceException("项目日历生成失败，请联系系统管理员！");
        }
        List<BasicEntity> pmCalendarEntities = new ArrayList<BasicEntity>();
        //遍历添加
        Date tempdate = startdate;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startdate);
        while (tempdate.getTime() < enddate.getTime()) {
            tempdate = calendar.getTime();
            //实体类字段赋值
            PmCalendarEntity pmCalendarEntity = new PmCalendarEntity();
            pmCalendarEntity.setProjectid(projectid);
            pmCalendarEntity.setProjectcode(projectcode);
            pmCalendarEntity.setCalendardate(tempdate);
            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {//周一
                pmCalendarEntity.setCalendartype(CboSystemConst.CboWorkingCalendarCategoryEnum.Workday);
                pmCalendarEntity.setDescription("星期一");
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {//周二
                pmCalendarEntity.setCalendartype(CboSystemConst.CboWorkingCalendarCategoryEnum.Workday);
                pmCalendarEntity.setDescription("星期二");
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {//周三
                pmCalendarEntity.setCalendartype(CboSystemConst.CboWorkingCalendarCategoryEnum.Workday);
                pmCalendarEntity.setDescription("星期三");
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {//周四
                pmCalendarEntity.setCalendartype(CboSystemConst.CboWorkingCalendarCategoryEnum.Workday);
                pmCalendarEntity.setDescription("星期四");
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {//周五
                pmCalendarEntity.setCalendartype(CboSystemConst.CboWorkingCalendarCategoryEnum.Workday);
                pmCalendarEntity.setDescription("星期五");
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {//周六
                pmCalendarEntity.setCalendartype(CboSystemConst.CboWorkingCalendarCategoryEnum.Workday);
                pmCalendarEntity.setDescription("星期六");
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {//周日
                pmCalendarEntity.setCalendartype(CboSystemConst.CboWorkingCalendarCategoryEnum.Weekend);
                pmCalendarEntity.setDescription("星期天");
            }
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            pmCalendarEntities.add(pmCalendarEntity);
        }
        List<BasicEntity> entities = new ArrayList<BasicEntity>();
        //数据批量插入数据库
        for (int i = 0; i < pmCalendarEntities.size(); i++) {
            entities.add(pmCalendarEntities.get(i));
            if (i > 0 && i % 500 == 0) {
                this.insertBatch(entities);
                entities.clear();
            }
        }
        if (entities.size() > 0) {
            this.insertBatch(entities);
        }
    }
}
